Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports Csla
Imports Csla.Data

<Serializable()> _
Public Class ro_coachRootList
    Inherits Csla.ReadOnlyListBase(Of ro_coachRootList, ro_coachChild)

#Region " Authorization Rules "

    Public Shared Function CanGetObject() As Boolean
        'TODO: Define CanGetObject permission in ro_coachRootList
        Return True
        'If Csla.ApplicationContext.User.IsInRole("ro_coachRootListViewGroup") Then
        '	Return True
        'End If
        'Return False
    End Function

#End Region ' Authorization Rules

#Region " Factory Methods "

    Private Sub New()
        'require use of factory method
    End Sub

    Public Shared Function Getro_coachRootList() As ro_coachRootList
        If Not CanGetObject() Then
            Throw New System.Security.SecurityException("User not authorized to view a ro_coachRootList")
        End If
        Return DataPortal.Fetch(Of ro_coachRootList)(New FilterCriteria())
    End Function

#End Region ' Factory Methods

#Region " Data Access "

#Region " Filter Criteria "

    <Serializable()> _
    Private Class FilterCriteria
        Public CoachId As Integer

        Public Sub New()

        End Sub
    End Class

#End Region

#Region " Data Access - Fetch "

    Private Overloads Sub DataPortal_Fetch(ByVal criteria As FilterCriteria)
        RaiseListChangedEvents = False
        IsReadOnly = False
        Dim myConnectionObj As New connection
        Dim myConnection As SqlClient.SqlConnection
        myConnection = myConnectionObj.ReadMySettings()

        Using cn As SqlConnection = New SqlConnection(myConnection.ConnectionString.ToString)
            cn.Open()
            ExecuteFetch(cn, criteria)
        End Using
        IsReadOnly = True
        RaiseListChangedEvents = True
    End Sub

    Private Sub ExecuteFetch(ByVal cn As SqlConnection, ByVal criteria As FilterCriteria)
        Using cm As SqlCommand = cn.CreateCommand()
            cm.CommandType = CommandType.StoredProcedure
            cm.CommandText = "sp_getCoachInfo"
            cm.Parameters.AddWithValue("@iFilter", 0)
            cm.Parameters.AddWithValue("@coachId", 0)
            Using dr As SafeDataReader = New SafeDataReader(cm.ExecuteReader())
                While dr.Read()
                    Me.Add(ro_coachChild.Getro_coachChild(dr))
                End While
            End Using
        End Using
    End Sub

#End Region ' Data Access - Fetch

#End Region ' Data Access

End Class
